package com.huluobo.a2208kotlindouyindemo.cart

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2208kotlindouyindemo.R
import com.huluobo.a2208kotlindouyindemo.adapter.CartAdapter
import com.huluobo.a2208kotlindouyindemo.databinding.ActivityCartBinding
import com.huluobo.base.bean.CartBean
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.parse
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class CartActivity : BaseActivity<ActivityCartBinding, CartViewModel>() {
    private lateinit var cartAdapter: CartAdapter
    private lateinit var cartBean: CartBean
    override fun initData() {
        vm.getCartList()
        vm.getCartSuccess.observe(this) {
            cartAdapter.data.clear()
            cartAdapter.data.addAll(it)
            cartAdapter.notifyDataSetChanged()
        }

        vm.addCartSuccess.observe(this) {
            ToastUtils.showLong("购物车数量+1")
        }

        vm.removeCartSuccess.observe(this) {
            ToastUtils.showLong("购物车数量-1")
        }

        vm.deleteCartSuccess.observe(this) {
            ToastUtils.showLong("移除购物车成功")
            cartAdapter.remove(cartBean)
        }
    }

    override fun initView() {
        binding.cartRv.layoutManager = LinearLayoutManager(this)
        cartAdapter = CartAdapter()
        binding.cartRv.adapter = cartAdapter
        totalPrice()

        cartAdapter.addChildClickViewIds(
            R.id.item_cart_cb,
            R.id.item_cart_add_btn,
            R.id.item_cart_remove_btn,
            R.id.item_cart_delete_iv
        )
        cartAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.item_cart_cb -> {
                    cartAdapter.data[position].isChecked = !cartAdapter.data[position].isChecked
                    (adapter.getViewByPosition(position, R.id.item_cart_cb) as CheckBox).isChecked =
                        cartAdapter.data[position].isChecked
                    checkAll()
                }

                R.id.item_cart_add_btn -> {
                    cartAdapter.data[position].count++
                    (adapter.getViewByPosition(position, R.id.item_cart_num_tv) as TextView).text =
                        "${cartAdapter.data[position].count}"
                    val map = mutableMapOf<String, Int>()
                    map[Const.PARAM_COUNT] = 1
                    map[Const.PARAM_GOODS_ID] = cartAdapter.data[position].goods_id
                    val json = Gson().toJson(map)
                    val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                    vm.addToCart(body)
                }

                R.id.item_cart_remove_btn -> {
                    if (cartAdapter.data[position].count > 1) {
                        cartAdapter.data[position].count--
                        (adapter.getViewByPosition(position, R.id.item_cart_num_tv) as TextView).text =
                            "${cartAdapter.data[position].count}"
                        val map = mutableMapOf<String, Int>()
                        map[Const.PARAM_COUNT] = -1
                        map[Const.PARAM_GOODS_ID] = cartAdapter.data[position].goods_id
                        val json = Gson().toJson(map)
                        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                        vm.removeCart(body)
                    }
                }

                R.id.item_cart_delete_iv -> {
                    cartBean = cartAdapter.data[position]
                    val map = mutableMapOf<String, String>()
                    map[Const.PARAM_IDS] = cartBean.id.toString()
                    val json = Gson().toJson(map)
                    val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                    vm.deleteCart(body)
                }
            }
            totalPrice()
        }

        binding.cartSelectAllCb.setOnClickListener {
            cartAdapter.data.forEach {
                it.isChecked = binding.cartSelectAllCb.isChecked
            }
            cartAdapter.notifyDataSetChanged()
            totalPrice()
        }
    }

    private fun totalPrice() {
        var totalPrice = 0

        cartAdapter.data.forEach {
            if (it.isChecked) {
                totalPrice += it.goods_default_price * it.count
            }
        }
        binding.cartTotalPriceTv.text = "总价:$totalPrice"
    }

    private fun checkAll() {
        val checkList = mutableListOf<Boolean>()
        cartAdapter.data.forEach {
            checkList.add(it.isChecked)
        }

        binding.cartSelectAllCb.isChecked = false !in checkList
    }
}